#include "edit_account.h"
#include "ui_edit_account.h"
#include<QDebug>
#include<QDesktopWidget>
#include<QMessageBox>
#include<QtSql/QSqlQuery>
//This class represents with list of all accounts either to edit or delete
//and accordingly either redirects user to newaccount to edit or deletes account

Edit_account::Edit_account(int pageid,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Edit_account)
{
    ui->setupUi(this);
    id=pageid;
    QRect innersize=QApplication::desktop()->availableGeometry(this);
   // ui->listWidget->setGeometry(0,50,(innersize.width()),(innersize.height()));
    ui->scrollArea->setGeometry(0,0,(innersize.width()),(innersize.height()));
    ui->listWidget->setEditFocus(true);
    int fontSize=10;
    int count=0;
    QAction* backAction = new QAction( tr("Back"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );

    if(db.db_connect())
    {
       // qDebug()<<"Connected";
        QSqlQuery qr;
               qr.prepare("select * from account_table");
               if(qr.exec())
               {
                   while(qr.next())
                   {
                    QString nm=qr.value(1).toString();
                   // qDebug()<<nm<<qr.value(0).toString()<<qr.value(1).toString()<<qr.value(2).toDouble()<<qr.value(3).toDouble();
                    item1=new QListWidgetItem(nm,ui->listWidget,QListWidgetItem::UserType);
                    item1->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));
                    item1->setToolTip(QString("%1").arg(qr.value(0).toString()));
                    count++;

               }
                }
               if(count==0)
               { ui->listWidget->hide();
                   ui->label->hide();
                   QMessageBox::information(0,"Error","No account is present");

               }

         db.db_close();
    }
}

Edit_account::~Edit_account()
{

    delete ui;
}
void Edit_account::back()
{
    account *acc=new account;
    acc->show();
}

void Edit_account::on_listWidget_itemActivated(QListWidgetItem* item)
{
    qDebug()<<item->toolTip().toInt();
    if(id==0)
        (new New_account(item->toolTip().toInt()))->show();
    else if(id==1)
    {
        QMessageBox msg;
       // msg.setText("It will delete expense permanently");
        msg.setInformativeText("Do you really want to delete ?");
        msg.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
        msg.setDefaultButton(QMessageBox::Yes);
        int res=msg.exec();
        if(res==QMessageBox::Yes)
        {
           if(db.db_connect())
           {
               if(db.db_account_delete(item->toolTip().toInt()))
               {
                   QMessageBox::information(0,"Success","Account deleted successfully");
                   (new account)->show();
               }
               else
                   QMessageBox::information(0,"Error","Error in deleting account");
               db.db_close();
           }
        }
    }
}
